﻿using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using DHJ.Cost.Tasks;
using DHJ.Cost.Timing;
using DHJ.Cost.Workshops;

namespace DHJ.Cost.CostBooking
{
    public class DirectCost : ActualCost
    {
        public DirectCost()
        {
        }

        public DirectCost(OccurrenceTime time, CostSubject subject, CostDetail detail, Workshop workshop,
            List<BatchIdentifier> batchIds) : base(time, subject, detail, workshop)
        {
            Batches = new BatchesString(batchIds);
        }

        public BatchesString Batches { get; private set; }

        protected override List<BatchRatio> CalculateBatchRatios(IBatchRatioCalculationContext context)
        {
            Debug.Assert(Workshop != null, nameof(Workshop) + " != null");
            var workshop = Workshop.Value;

            var total = Batches.Identifiers.Sum(b => b.Quantity);
            return Batches.Identifiers.Select(b =>
                new BatchRatio
                {
                    BatchId = b.BatchId,
                    Quantity = b.Quantity,
                    Workshop = workshop,
                    Ratio = (decimal)b.Quantity / total
                }).ToList();
        }
    }
}
